﻿package edzis.translatebuddy.view.components {
	import edzis.translatebuddy.events.DataProxyEvent;
	import edzis.translatebuddy.model.vo.SearchProfileVO;
	import edzis.translatebuddy.model.vo.WordVO;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.data.DataProvider;
	import fl.events.ComponentEvent;
	import fl.events.DataChangeEvent;
	import fl.events.ScrollEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.html.HTMLLoader;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	import mx.controls.HTML;
	
	/**
	 * The View Component for adding a word to the translation list
	 * @author Edgars Simsons
	 */
	public class TranslateWord extends TranslateWord_design {
		private var _enterDown	:Boolean;
		private var htmlContent:HTMLLoader;
		private var _searchPhrase:String = "none";
	//	private var htmlContent:HTML;
		
		public function TranslateWord() {
			visible = false;
			searchProfileList.addEventListener(Event.CHANGE, onProfileChange);
			wordList.addEventListener(Event.CHANGE, onSelectionChange);
			addBtn.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			addBtn.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			addBtn.addEventListener(MouseEvent.CLICK, onAddTranslation);
			
		//	htmlContent = new HTML();
			htmlContent = new HTMLLoader();
			htmlContent.name = "htmlContent";
			htmlContent.y = 140;
			htmlContent.width = 545;
			htmlContent.height = 200;
			htmlContent.addEventListener(Event.SCROLL, onHtmlScroll);
			htmlContent.addEventListener(Event.HTML_BOUNDS_CHANGE, onHtmlBoundsChange);
			htmlContent.addEventListener(Event.HTML_RENDER, onHtmlRender);
			htmlContent.addEventListener(Event.HTML_DOM_INITIALIZE, onHtmlDomInitialize);
			htmlContent.addEventListener(Event.LOCATION_CHANGE, onLocationChange);
			addChild(htmlContent);
			if (stage) onAddedToStage(null);
			else addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			htmlScroller.scrollTargetName = "htmlContent";
			htmlScroller.y = htmlContent.y;
			htmlScroller.addEventListener(ScrollEvent.SCROLL, onScrollBarScroll);
			
			if(stage) onResize(null);
		}
		
		private function onScrollBarScroll(e:ScrollEvent):void {
			htmlContent.scrollV = e.position;
		}
		
		private function onHtmlScroll(e:Event):void {
			htmlScroller.scrollPosition = htmlContent.scrollV;
		}
		
		private function onHtmlBoundsChange(e:Event):void {
			ArthropodDebug.log("onHtmlBoundsChange " + htmlContent.contentWidth + " " + htmlContent.contentHeight);
			if(htmlContent.contentWidth != 0 && htmlContent.contentHeight != 0) htmlContent.alpha = 1;
			htmlScroller.setScrollProperties(htmlContent.contentHeight, 0, htmlContent.contentHeight - htmlContent.height, 0);
			if (stage) onResize();
		}
		
		private function onHtmlRender(e:Event):void {
			ArthropodDebug.log("onHtmlRender "  + "");
		}
		
		private function onHtmlDomInitialize(e:Event):void {
			ArthropodDebug.log("onHtmlDomInitialize "  + "");
		}
		
		private function onLocationChange(e:Event):void {
			ArthropodDebug.log("onLocationChange "  + htmlContent.location);
			htmlContent.alpha = 0.5;
		}
		
		private function onSelectionChange(e:Event):void {
			if(wordList.selectedItem) {
				translationTxt.text = (wordList.selectedItem as WordVO).translation;
				browseForTranslation((wordList.selectedItem as WordVO).word);
			}
		}
		private function onProfileChange(e:Event):void {
			ArthropodDebug.log("searchProfileList " + searchProfileList.selectedIndex + " of " + searchProfileList.dataProvider.length);
			_searchPhrase = (searchProfileList.selectedItem)? (searchProfileList.selectedItem as SearchProfileVO).url : "none";
			if(wordList.selectedItem) browseForTranslation((wordList.selectedItem as WordVO).word);
			if (_searchPhrase == "none") {
				htmlScroller.visible = false;
				htmlContent.visible = false;
			} else {
				if(!htmlScroller.visible) htmlScroller.visible = true;
				if(!htmlContent.visible) htmlContent.visible = true;
			}
		}
		
		private function browseForTranslation(phrase:String):void{
			//htmlContent.location = "http://fr.wiktionary.org/wiki/Special:Search?search=" + phrase;
			//htmlContent.htmlLoader = htmlContent.htmlLoaderFactory;
			if(_searchPhrase != "none") {
				if (_searchPhrase == null) _searchPhrase = (searchProfileList.selectedItem as SearchProfileVO).url;
				try {
					htmlContent.cancelLoad();
				} catch (e:Error){
					ArthropodDebug.error("Could not cancel loading : " + e);
				}
				
				try {
					htmlContent.load(new URLRequest(_searchPhrase.replace("#PHRASE#", escape(phrase))));
				} catch (e:Error){
					ArthropodDebug.error("Could not start loading : " + e);
				}
			}
		}
		
		private function onAddTranslation(e:MouseEvent):void {
		//	ArthropodDebug.log("wordTxt.selectedLabel" + wordTxt.selectedLabel);
			if (translationTxt.text != "") {
				ArthropodDebug.log("onAddTranslation " + translationTxt.text);
				dispatchEvent( new DataProxyEvent(DataProxyEvent.ADD_TRANSLATION, {wordID:(wordList.selectedItem as WordVO).id, translation:translationTxt.text}));
				translationTxt.text = "";
				stage.focus = translationTxt;
				wordList.selectedIndex = wordList.selectedIndex + 1;
				wordList.scrollToSelected();
			}
		}
		
		public function init(allWords:DataProvider, allSearchProfiles:DataProvider) :void {
			wordList.dataProvider = allWords;
			searchProfileList.dataProvider = allSearchProfiles;
			wordList.dataProvider.addEventListener(DataChangeEvent.DATA_CHANGE, onWordListDataChange);
			searchProfileList.dataProvider.addEventListener(DataChangeEvent.DATA_CHANGE, onSearchProfileDataChange);
		}
		
		private function onWordListDataChange(e:DataChangeEvent):void {
			ArthropodDebug.log("onWordListDataChange");
			if (wordList.selectedIndex == -1 && wordList.dataProvider.length > 1) wordList.selectedIndex = 0;
			onSelectionChange(null);
		}
		
		private function onSearchProfileDataChange(e:DataChangeEvent):void {
			ArthropodDebug.log("onSearchProfileDataChange");
			if (searchProfileList.selectedIndex == -1 && searchProfileList.dataProvider.length > 1) searchProfileList.selectedIndex = 0;
			onProfileChange(null);
		}
		
		private function onAddedToStage(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			stage.addEventListener(Event.RESIZE, onResize);
			onResize(null);
		}
		
		private function onResize(e:Event = null):void {
			htmlContent.width = stage.stageWidth - x*2 - 15;
			htmlContent.height = stage.stageHeight - y - x - htmlContent.y;
			htmlScroller.height = htmlContent.height;
			htmlScroller.x = htmlContent.width;
		}
		
		private function onWordChanged(e:Event):void {
			//
			trace("wordTxt.selectedItem " + wordList.selectedItem);
		//	if (wordTxt.selectedItem != "")
			
		}
		
		private function onKeyDown(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.ENTER && !_enterDown) {
				_enterDown = true;
				onAddTranslation(null);
			}
		}
		
		private function onKeyUp(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.ENTER) _enterDown = false;
		}
		
		private function onWordUpdate(e:Event):void{
		}
		
		private function onTranslationUpdate(e:Event):void{
			
		}
		
	}
	
}